//
//  LHChannelView.m
//  channelDemo
//
//  Created by zhouLiHuan on 2017/2/6.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "LHChannelView.h"
#import "LHChannelItem.h"
#import "LHChannelTitleView.h"
#import "LHChannelControl.h"

//菜单列数
static NSInteger ColumnNumber = 4;

//横向和纵向的间距
static CGFloat BtnMarginX = 15.0f;
static CGFloat BtnMarginY = 10.0f;

@interface LHChannelView()
{
    //保存上半部分卡片
    NSMutableArray *_inUseItems;
    //保存下半部分卡片
    NSMutableArray *_unUserItems;
    
    //上半部分标题
    LHChannelTitleView *_inUseTitleView;
    //下半部分标题
    LHChannelTitleView *_unUseTitleView;
    
    //滚动的view
    UIScrollView *_scrollView;
    //被拖动的卡片
    LHChannelItem *_dragingItem;
    //空白卡片
    LHChannelItem *_palceholderItem;
    //目标卡片
    LHChannelItem *_targetItem;
    
}

@end

@implementation LHChannelView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self buildUI];
    }
    return self;
}

- (void)buildUI{
    [self addSubview:[UIView new]];
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_scrollView];
    
    //上半部分标题
    _inUseTitleView = [[LHChannelTitleView alloc] initWithFrame:CGRectMake(BtnMarginX, BtnMarginY, self.bounds.size.width - 2*BtnMarginX, [self titleViewHeight])];
    _inUseTitleView.title = @"已选频道";
    _inUseTitleView.subTitle = @"按住拖动调整排序";
    [_scrollView addSubview:_inUseTitleView];
    
    _inUseItems = [NSMutableArray new];
    _unUserItems = [NSMutableArray new];
    
    //初始化上部分的菜单按钮
    for (int i = 0; i < [LHChannelControl shareControl].inUseItems.count; i ++) {
        LHChannelModel *model = [LHChannelControl shareControl].inUseItems[i];
        LHChannelItem *item = [[LHChannelItem alloc] initWithFrame:[self inUserItemFrameOfaIndex:i]];
        item.title = model.title;
        [_scrollView addSubview:item];
        [_inUseItems addObject:item];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTapMethod:)];
        [item addGestureRecognizer:tap];
 
    }
    
    
    //下半部分标题
    _unUseTitleView = [[LHChannelTitleView alloc] initWithFrame:CGRectMake(BtnMarginX, [self unUseLabelY], self.bounds.size.width - 2*BtnMarginX, [self titleViewHeight])];
    _unUseTitleView.title = @"推荐频道";
    [_scrollView insertSubview:_unUseTitleView atIndex:0];
    
    //初始化下部分的菜单按钮
    for (int i = 0; i < [LHChannelControl shareControl].unUseItems.count; i ++) {
        LHChannelModel *model = [LHChannelControl shareControl].unUseItems[i];
        LHChannelItem *item = [[LHChannelItem alloc] initWithFrame:[self unUseItemFrameOfIndex:i]];
        item.title = model.title;
        [_scrollView addSubview:item];
        [_unUserItems addObject:item];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTapMethod:)];
        [item addGestureRecognizer:tap];
        
        _scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(item.frame) + BtnMarginY);
    }
    
    //添加占位按钮
    _palceholderItem = [[LHChannelItem alloc] initWithFrame:CGRectZero];
    _palceholderItem.isPlaceholder = YES;
    [_scrollView addSubview:_palceholderItem];
    
    //添加长按编辑方法
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMethod:)];
    longPress.minimumPressDuration = 0.3f;
    [_scrollView addGestureRecognizer:longPress];

}

#pragma mark - 拖拽方法
- (void)longPressMethod:(UIGestureRecognizer *)recognizer{
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
        {
            [self gestureBegan:recognizer];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            [self gestureChanged:recognizer];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [self gesTureEnd:recognizer];
        }
            break;
        default:
            break;
    }
}

//开始拖拽
- (void)gestureBegan:(UIGestureRecognizer *)gesture{
    //不允许scrollview滚动
    _scrollView.scrollEnabled = NO;
    CGPoint point = [gesture locationInView:_scrollView];
    
    //获取被拖拽的卡片
    _dragingItem = [self getDragingitemWithPoint:point];
    
    //拖拽是空则不执行下面方法
    if (!_dragingItem) {
        return;
    }
    //显示放大
    [self showAnimation:_dragingItem bigger:YES];
    
    //设置占位按钮的标题
    _palceholderItem.title = _dragingItem.title;
    //用空白的方块代替拖拽的方块
    NSInteger index = [_inUseItems indexOfObject:_dragingItem];
    [_inUseItems replaceObjectAtIndex:index withObject:_palceholderItem]; //?????
    _palceholderItem.frame = [self inUserItemFrameOfaIndex:index];
    
    //把被拖拽的方块移动到最前面
    [_scrollView bringSubviewToFront:_dragingItem];
    
    //更新UI
    [self upDateUI];
    
}

//拖拽中
- (void)gestureChanged:(UIGestureRecognizer *)gesture{
    //没有被拖拽的按钮 不执行下面方法
    if (!_dragingItem) {
        return;
    }
    
    //更新被拖拽按钮的frame
    CGPoint point = [gesture locationInView:self];
    _dragingItem.center = point;
    
    //获取准备要交换位置的菜单按钮
    _targetItem = [self getTargetitemWithPoint:point];
    
    //没找到准备交换的目标卡片则不执行下面方法
    if (!_targetItem) {
        return;
    }
    
    //交换空白块和目标块的位置
    [_inUseItems removeObject:_palceholderItem];
    NSInteger index = [_inUseItems indexOfObject:_targetItem];
    if (_palceholderItem.frame.origin.y == _targetItem.frame.origin.y) {
        if (_dragingItem.center.x < _targetItem.center.x) {
            index += 1;
        }
    }else if (_palceholderItem.frame.origin.y < _targetItem.frame.origin.y){
        index += 1;
    }
    
    [_inUseItems insertObject:_palceholderItem atIndex:index];
    
    //更新位置
    [self upDateUI];
}

//拖拽结束
- (void)gesTureEnd:(UIGestureRecognizer *)gesture{
    //允许Scrollview滚动
    _scrollView.scrollEnabled = YES;
    
    //没有找到合适的拖拽按钮,就不执行下面方法
    if (!_dragingItem) {
        return;
    }
    
    //和占位按钮交换位置
    [_inUseItems replaceObjectAtIndex:[_inUseItems indexOfObject:_palceholderItem] withObject:_dragingItem];
    [self showAnimation:_dragingItem bigger:NO];
    
    //更新UI
    [self upDateUI];
    [self saveData];
}


//动画效果
- (void)showAnimation:(LHChannelItem *)item bigger:(BOOL)bigger{
    CGFloat scale = bigger ? 1.3f : 1.0f;
    [UIView animateWithDuration:0.3 animations:^{
        CGAffineTransform newTransform = CGAffineTransformMakeScale(scale, scale);
        [item setTransform:newTransform];
    } completion:^(BOOL finished) {
        
    }];
    
}


#pragma mark - 辅助方法

//获取被拖拽的方块
- (LHChannelItem *)getDragingitemWithPoint:(CGPoint)point{
    LHChannelItem *item = nil;
    
    if (_inUseItems.count == 1) {
        return item;
    }
    
    CGRect checkRect = CGRectMake(point.x, point.y, 1, 1);
    for (LHChannelItem *enumitem in _scrollView.subviews) {
        if (![enumitem isKindOfClass:[LHChannelItem class]]) {
            continue;
        }
        if (![_inUseItems containsObject:enumitem]) {
            continue;
        }
        if (CGRectIntersectsRect(enumitem.frame, checkRect)) {
            item = enumitem;
            break;
        }
    }
    return item;
    
}

//获取目标位置方块
- (LHChannelItem *)getTargetitemWithPoint:(CGPoint)point{
    LHChannelItem *targetitem = nil;
    for (LHChannelItem *enumItem in _scrollView.subviews) {
        if (![enumItem isKindOfClass:[LHChannelItem class]]) {
            continue;
        }
        if (enumItem == _dragingItem) {
            continue;
        }
        
        if (enumItem == _palceholderItem) {
            continue;
        }
        
        if (![_inUseItems containsObject:enumItem]) {
            continue;
        }
        if (CGRectContainsPoint(enumItem.frame, point)) {
            targetitem = enumItem;
        }
    }
    return targetitem;
}

#pragma mark - 点击方法
- (void)itemTapMethod:(UIGestureRecognizer *)gesture{
    LHChannelItem *item = (LHChannelItem *)gesture.view;
    [_scrollView bringSubviewToFront:item];
    
    //更新数据源的数据
    if ([_inUseItems containsObject:item]) {
        NSLog(@"这是上面的");
        //只有一个的时候不能删除
        if (_inUseItems.count == 1) {
            return;
        }
        [_inUseItems removeObject:item];
        [_unUserItems insertObject:item atIndex:0];
    }else{
        NSLog(@"这是下面的");
        [_unUserItems removeObject:item];
        [_inUseItems addObject:item];
    }
    //更新UI
    [self upDateUI];
    [self saveData];

}

#pragma mark - 定义按钮和其他控件的宽高
- (CGFloat)itemWidth{
    return (self.bounds.size.width - (ColumnNumber + 1)*BtnMarginX)/ColumnNumber;
    
}

- (CGFloat)itemHeight{
    return [self itemWidth]/2.0f;
}

- (CGFloat)titleViewHeight{
    return 0.09 * self.bounds.size.width;
}

- (CGFloat)unUseLabelY{
    NSInteger count = _inUseItems.count;
    CGFloat y = [self titleViewHeight];
    if (count > 0) {
        y = CGRectGetMaxY([self inUserItemFrameOfaIndex:_inUseItems.count - 1]) + BtnMarginY;
    }
    return y;
}

#pragma mark - 更新UI方法
- (void)upDateUI{
    [UIView animateWithDuration:0.35 animations:^{
        [self updateUnUseLabelFrame];
        [self updateItemFrame];
        
    } completion:^(BOOL finished) {
        if (![_inUseItems containsObject:_palceholderItem]) {
            _palceholderItem.frame = CGRectZero;
        }
    }];
}

//更新每个label的范围
- (void)updateItemFrame{
    //更新正在使用的item的范围
    for (int i = 0;i < _inUseItems.count; i ++) {
        LHChannelItem *item = _inUseItems[i];
        item.frame = [self inUserItemFrameOfaIndex:i];
        
    }
    
    //更新未使用的item的范围
    for (int i = 0; i < _unUserItems.count; i ++) {
        LHChannelItem *item = _unUserItems[i];
        item.frame = [self unUseItemFrameOfIndex:i];
        _scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(item.frame) + BtnMarginY);
    }
    
}

//使用中item的frame

- (CGRect)inUserItemFrameOfaIndex:(NSUInteger)index{
    CGFloat itemY = CGRectGetMaxY(_inUseTitleView.frame) + index/ColumnNumber * [self itemHeight] + (index/ColumnNumber +1)*BtnMarginY;
    CGFloat itemX = index%ColumnNumber*[self itemWidth] + (index%ColumnNumber +1)*BtnMarginX;
    return CGRectMake(itemX, itemY, [self itemWidth], [self itemHeight]);
    
}

//未使用中的item的frame
- (CGRect)unUseItemFrameOfIndex:(NSInteger)index{
    CGFloat itemY = [self unUseLabelY] + [self titleViewHeight] + index/ColumnNumber * [self itemHeight] + (index/ColumnNumber + 1)*BtnMarginY;
    CGFloat itemX = index%ColumnNumber * [self itemWidth] + (index%ColumnNumber + 1)*BtnMarginX;
    
    return CGRectMake(itemX, itemY, [self itemWidth], [self itemHeight]);
}

// 标题栏的frame
- (void)updateUnUseLabelFrame{
    CGRect frame = _unUseTitleView.frame;
    frame.origin.y = [self unUseLabelY];
    _unUseTitleView.frame = frame;
}

/**
 保存本地数据
 */
- (void)saveData{
    NSMutableArray *arr1 = [NSMutableArray new];
    NSMutableArray *arr2 = [NSMutableArray new];
    for (int i = 0; i < _inUseItems.count; i ++) {
        LHChannelModel *model = [LHChannelModel new];
        LHChannelItem *item = _inUseItems[i];
        model.title = item.title;
        [arr1 addObject:model];
    }
    
    for (int i = 0; i < _unUserItems.count; i ++) {
        LHChannelModel *model = [LHChannelModel new];
        LHChannelItem *item = _unUserItems[i];
        model.title = item.title;
        [arr2 addObject:model];
    }
    
    [LHChannelControl shareControl].inUseItems = arr1;
    [LHChannelControl shareControl].unUseItems = arr2;
}


@end
